##### Descriptive plots        #----------------------------------------------#

# This script generates all descriptive plots based on the BTM

#------------- Timeline of events (Figure 1) --------------------------------------------

# Load protest data 
load("output/all_protests_sub.Rda")

# Plot share of Covid-related protests over time
all_protests_monthly <- all_protests_sub %>% 
  mutate(year_month = yearmonth(event_date)) %>% 
  group_by(year, year_month) %>% # aggregate at year month level
  summarise(monthly_events = n(),
            monthly_pandem_events = sum(corona_topic, na.rm = T),
            monthly_non_pandem_events = monthly_events - monthly_pandem_events) %>% 
  mutate(share_pandemic =  (monthly_pandem_events / monthly_events)* 100) %>% 
  ungroup() %>% 
  pivot_longer(cols = c(monthly_events:share_pandemic),
               names_to = "variable")  %>% 
  filter(variable != "monthly_events")

# Plot events over time 
ggplot(all_protests_monthly %>% filter(variable != "share_pandemic" & year %in% c(2019,2020,2021,2022)),
       aes(x = year_month, y = value,
           fill = factor(variable))) + 
  geom_area(alpha = 0.55, color = "black", show.legend = FALSE) +
  scale_y_continuous("Protest events", breaks = seq(0,21000,3000)) +
  scale_x_yearmonth("", date_breaks = "3 months", date_labels = "%b %Y") +
  scale_fill_manual(values= c("grey70", "grey20")) +
  theme_bw() +
  theme(axis.text.x = element_text(angle=45, hjust=1)) +
  geom_vline(xintercept = lubridate::ymd("2020 Feb", truncated = 1), lty = "dotted") +
  geom_vline(xintercept = lubridate::ymd("2020 Apr", truncated = 1), lty = "dotted") +
  geom_vline(xintercept = lubridate::ymd("2020 Sep", truncated = 1), lty = "dotted") +
  geom_vline(xintercept = lubridate::ymd("2021 May", truncated = 1), lty = "dotted") +
  geom_vline(xintercept = lubridate::ymd("2021 Nov", truncated = 1), lty = "dotted") +
  annotate(geom = "text", x = lubridate::ymd("2020 Jan", truncated = 1), y = 15000, alpha = .75, label = '"Global health emergency"',  angle = 90, size = 4) +
  annotate(geom = "text", x = lubridate::ymd("2020 Mar", truncated = 1), y = 14000, alpha = .75, label = "Global cases exceed 1 Mio.", angle = 90, size = 4) +
  annotate(geom = "text", x = lubridate::ymd("2020 Aug", truncated = 1), y = 8000, alpha = .75, label = "Global deaths reach 1 Mio.", angle = 90, size = 4) +
  annotate(geom = "text", x = lubridate::ymd("2021 Apr", truncated = 1), y = 8000, alpha = .75, label = "Delta variant detected", angle = 90, size = 4) +
  annotate(geom = "text", x = lubridate::ymd("2021 Oct", truncated = 1), y = 8000, alpha = .75, label = "Omicron variant detected", angle = 90, size = 4) 

# Save plot
ggsave("output/Figure_1.pdf", width = 7, height = 5.5, dpi = "retina")

#------------- Covid-related topics over time (Figure 2) --------------------------------------------

# Load topic scores
load("output/acled_btm.Rda")

# Aggregate topics at the year-week level
acled_btm_sub <- acled_btm %>% 
  group_by(year_week) %>% 
  summarise(across(health_care:education, 
                   ~ mean(., na.rm = T))) %>% 
  pivot_longer(cols = c(health_care:education), names_to = "topic") %>% 
  ungroup() %>% 
  mutate(topic_label = case_when(topic == "economic_consequences" ~ "economy",
                                 topic == "imprisonment_crime" ~ "imprisonment and crime",
                                 topic == "vaccination" ~ "vaccination",
                                 topic == "education" ~ "education",
                                 topic == "health_care" ~ "health care",
                                 topic == "mishandling_corruption" ~ "mismanagement",
                                 topic == "restrictions_masks" ~ "restrictions/masks",
                                 topic == "business_restrictions" ~ "business restrictions",
                                 T ~ NA_character_))
# PLot topic prevalence over time
ggplot(acled_btm_sub ,
       aes(x = as.POSIXct(year_week),
           y = value, 
           group = factor(topic_label))) +
  #  scale_color_brewer(breaks = c(3,2,1,0),labels = c("liberal dem.", "electoral dem",
  #                                "electoral aut.", "closed aut"),type = "qual") +
  geom_point(color = "grey", fill = "white", alpha = .65) +
  geom_smooth(color = "black", span = .55) +
  scale_y_continuous("beta") +
  scale_x_yearweek("", date_breaks = "3 months", date_labels = "%b %Y") +
  geom_vline(xintercept = as.POSIXct(yearweek("2020 W10")), lty = "dashed" )+
  theme(legend.position = "none",
        axis.text.x = element_text(angle=45, hjust=1)) +
  facet_wrap(~ topic_label, scales = "fixed", ncol = 2)

ggsave("output/Figure_2.pdf", width = 7, height = 6, dpi = "retina")

#------------- Lollipop plots for selected countries (Figure 3) --------------------------------------------


# Select countries to be plotted
countries <- c("Germany",
               "United States",
               "Brazil",
               "United Kingdom",
               "Italy",
               "South Africa",
               "Venezuela",
               "India",
               "Turkey")

# Reshape data
acled_during_sub <- acled_btm %>% 
  filter(country %in% countries) %>% 
  select(country, health_care:education)  %>%
  pivot_longer(
    cols = c(health_care:education),
    names_to = "topic",
    values_to = "mean_beta") %>% 
  group_by(country, topic) %>% 
  summarise(mean_beta = mean(mean_beta, na.rm = T)) %>% 
  ungroup() %>% 
  mutate(topic_label = case_when(topic == "economic_consequences" ~ "economy",
                                 topic == "imprisonment_crime" ~ "imprisonment/crime",
                                 topic == "vaccination" ~ "vaccination",
                                 topic == "education" ~ "education",
                                 topic == "health_care" ~ "health care",
                                 topic == "mishandling_corruption" ~ "mismanagement",
                                 topic == "restrictions_masks" ~ "restrictions/masks",
                                 topic == "business_restrictions" ~ "business restrictions",
                                 T ~ NA_character_)) %>% 
  mutate(country = factor(country, levels = c("Germany",
                                              "United States",
                                              "Brazil",
                                              "United Kingdom",
                                              "Italy",
                                              "South Africa",
                                              "Venezuela",
                                              "India",
                                              "Turkey")))

# plot
ggdotchart(
  acled_during_sub, x = "topic_label", y = "mean_beta", 
  group = "country", color = "topic", palette = "lancet",
  add = "segment", position = position_dodge(0.3),
  sorting = "descending", facet.by = c("country"),
  rotate = TRUE, legend = "none", xlab = "", ylab = "mean beta"
)

ggsave(filename = "output/Figure_3.pdf",
       width = 8, height = 5, dpi = "retina")


#------------- Covid-related topics over time: alternative visualization (Figure A1) --------------------------------------------

# aggregate topics at monthly level
acled_btm_sub_mon <- acled_btm_sub %>% 
  mutate(year_month = yearmonth(year_week)) %>% 
  group_by(year_month, topic, topic_label) %>% 
  summarise(mean_value = mean(value, na.rm = T)) %>% 
  ungroup()

# Plot topics over time in one plot
ggplot(acled_btm_sub_mon,
       aes(x = year_month, y = mean_value,
           fill = factor(topic_label))) + 
  geom_area(alpha = 0.55, color = "black") +
  scale_y_continuous("Topic share", breaks = seq(0,1,0.2)) +
  scale_x_yearmonth("", date_breaks = "3 months", date_labels = "%b %Y") +
  scale_fill_manual("",values=met.brewer("OKeeffe1", 8)) +
  theme(axis.text.x = element_text(angle=45, hjust=1)) 

ggsave("output/Figure_A1.pdf", width = 7, height = 5, dpi = "retina")
